//
//  MineViewController.m
//  news
//
//  Created by imacbookair on 16/3/9.
//  Copyright © 2016年 imacbookair. All rights reserved.
//

#import "MineViewController.h"
#import "PrefixHeader.pch"
@interface MineViewController ()<UITableViewDataSource, UITableViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong)UITableView *userTableView;
@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong)NSMutableArray *dataArr;
@property (nonatomic, strong)NSMutableArray *timuArr;
@property (nonatomic, strong)NSMutableArray *tuArr;
@property (nonatomic, strong)UIView *header;
@property (nonatomic, strong)NSIndexPath *index;

@end

@implementation MineViewController
- (void)viewWillAppear:(BOOL)animated
{
//    [self.userTableView reloadData];
    self.tabBarController.tabBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.userTableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
//    _userTableView.backgroundColor = [UIColor redColor];
    [self.view addSubview:_userTableView];
    [_userTableView NightWithType:UIViewColorTypeNormal];
    _userTableView.delegate = self;
    _userTableView.dataSource = self;
    
    /** 添加头部区域 **/
    self.header = [[UIView alloc]initWithFrame:CGRectMake(0, 30, 0, 200)];
    _header.backgroundColor = [UIColor redColor];
    _userTableView.tableHeaderView = _header;

    /** collectionView **/
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake((self.view.frame.size.width - 3 )/ 4, 60);
    //横向滑动
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    // 列间距
    layout.minimumLineSpacing = 1;
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 140, self.view.frame.size.width, 60)collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    // 允许滚动
    _collectionView.pagingEnabled = YES;
    /** 重用池 **/
    [_collectionView registerClass:[UserCollectionViewCell class] forCellWithReuseIdentifier:@"Toucell"];
    [_header addSubview:_collectionView];
    
    /** 登录按钮 **/
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(0, 0, 80, 30);
    button.center = CGPointMake(self.view.center.x, 110);
    [_header addSubview:button];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setTitle:@"立即登录" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
    
    // 添加图片
    UIImageView *touxiang = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 60, 60)];
    touxiang.layer.cornerRadius = 30;
    touxiang.backgroundColor = [UIColor whiteColor];
    touxiang.center = CGPointMake(self.view.center.x, 50);
    touxiang.image = [UIImage imageNamed:@"wo"];
    [_header addSubview:touxiang];
    
    /** 数据处理 */
    self.dataArr = [@[@"夜间模式",@"本地推送",@"清除缓存",@"离线阅读",@"发送消息", @"联系我们"]mutableCopy];
    self.timuArr = [@[@"阅读", @"收藏", @"跟帖", @"金币"]mutableCopy];
    self.tuArr = [@[@"shijian", @"xin", @"bi", @"jinbi"]mutableCopy];
    
    // tableView重用池
    [_userTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    
    
    
}
#pragma mark - 登录按钮实现方法
- (void)button:(UIButton *)btn{

    NSLog(@"sdsdsdsd");
    
}

#pragma mark - collectionView协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _timuArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UserCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Toucell" forIndexPath:indexPath];
    [cell NightWithType:UIViewColorTypeNormal];
    cell.backgroundColor = [UIColor whiteColor];
    cell.lable.text = _timuArr[indexPath.row];
    cell.tu.image = [UIImage imageNamed:_tuArr[indexPath.row]];
    
    return cell;
}


#pragma mark - tableView协议方法的实现
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 6;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    [cell NightWithType:UIViewColorTypeNormal];
    cell.textLabel.text = _dataArr[indexPath.row];
    [cell.textLabel NightWithType:UIViewColorTypeNormal];
    cell.textLabel.font = [UIFont systemFontOfSize:18];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (indexPath.row == 0) {
        // 创建开关
        UISwitch *witch = [[UISwitch alloc]initWithFrame:CGRectMake(280, 0, 80, 50)];
        witch.backgroundColor = [UIColor whiteColor];
        witch.layer.cornerRadius = 30;
        // 开状态下的颜色
        witch.onTintColor = [UIColor redColor];
        // 开关的颜色
//        witch.thumbTintColor = [UIColor blueColor];
        //witch  添加到cell 上
        cell.accessoryView = witch;
        // 添加事件
        [witch addTarget:self action:@selector(witchAction:) forControlEvents:UIControlEventValueChanged];
        
    }
    if(indexPath.row == 2){
        UILabel *clearLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width/3 * 2, 0, 100, cell.frame.size.height)];
        clearLabel.tag = 8989;
        
        [cell addSubview:clearLabel];
        clearLabel.textAlignment = NSTextAlignmentRight;
        NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
        
        CGFloat h = [ClearCache folderSizeAtPath:path];
        clearLabel.text = [NSString stringWithFormat:@"%.2fMB",h];
        
    }
    
    return cell;
    
}

// 开关触发夜间模式
- (void)witchAction:(UISwitch *)open{
    [ThemeManage shareThemeManage].isNight = ![ThemeManage shareThemeManage].isNight;
    [[NSNotificationCenter defaultCenter]postNotificationName:@"changeColor" object:nil];
    [[NSUserDefaults standardUserDefaults]setBool:[ThemeManage shareThemeManage].isNight forKey:@"night"];
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setBool:open.on forKey:@"switch"];

}

#pragma mark 点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    /** 清除缓存 **/
    if (indexPath.row == 2) {
        UIAlertController *aler = [UIAlertController alertControllerWithTitle:nil message:@"亲!你确定要抛弃我" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *quxiao = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
        UIAlertAction *queding = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
            [ClearCache clearCache:path];
            UILabel *label = [self.view viewWithTag:8989];
            [label removeFromSuperview];
            self.index = indexPath;
            [self.userTableView reloadRowsAtIndexPaths:@[_index] withRowAnimation:UITableViewRowAnimationNone];
        }];
        [aler addAction:quxiao];
        [aler addAction:queding];
        [self presentViewController:aler animated:YES completion:^{
        }];
        
}
    if (indexPath.row == 1) {
        UIAlertController *push = [UIAlertController alertControllerWithTitle:nil message:@"您已经开启推送功能" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        
        [push addAction:sure];
        [self presentViewController:push animated:YES completion:^{
            
        }];
    }
    
    if (indexPath.row == 3) {
        UIAlertController *push = [UIAlertController alertControllerWithTitle:nil message:@"敬请期待下一版本" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        
        [push addAction:sure];
        [self presentViewController:push animated:YES completion:^{
            
        }];
    }
    if (indexPath.row == 4) {
        UIAlertController *push = [UIAlertController alertControllerWithTitle:nil message:@"敬请期待下一版本" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        
        [push addAction:sure];
        [self presentViewController:push animated:YES completion:^{
            
        }];
    }
    if (indexPath.row == 5) {
        UIAlertController *push = [UIAlertController alertControllerWithTitle:nil message:@"敬请期待下一版本" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        
        [push addAction:sure];
        [self presentViewController:push animated:YES completion:^{
            
        }];
    }
}

#pragma mark - collectionView点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 1) {
    // 跳转收藏页面
    ShouCangViewController *shouCang = [[ShouCangViewController alloc]init];
    [self.navigationController pushViewController:shouCang animated:YES];
        
    }
    if (indexPath.row == 3) {
        UIAlertController *push = [UIAlertController alertControllerWithTitle:nil message:@"敬请期待下一版本" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        
        [push addAction:sure];
        [self presentViewController:push animated:YES completion:^{
            
        }];
    }
    if (indexPath.row == 0) {
        UIAlertController *push = [UIAlertController alertControllerWithTitle:nil message:@"敬请期待下一版本" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        
        [push addAction:sure];
        [self presentViewController:push animated:YES completion:^{
            
        }];
    }

    if (indexPath.row == 2) {
        UIAlertController *push = [UIAlertController alertControllerWithTitle:nil message:@"敬请期待下一版本" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        
        [push addAction:sure];
        [self presentViewController:push animated:YES completion:^{
            
        }];
    }
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
